<?php

/**
 * page actions.
 *
 * @package    DUYTAN
 * @subpackage page
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class cmsListAction extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeCmsList(sfWebRequest $request)
  {
	$this->notify = $request->getParameter("notify");
	$cms_temp = new VTNSCms();
	$cms_temp->cms_category_id = $request->getParameter('cms_category_id');
	$this->cms_form = new AdminCmsForm($cms_temp);
	if ($request->hasParameter('create') && $request->getParameter('create') == 'Create') {
		$res = $request->getParameter('cms'); // get form cms
        $this->cms_category_id = $res['cms_category_id'];
		$this->is_create_action = true;
		if ($res['name'] != '') {
			$cms_temp->name = $res['name'];
			$cms_temp->cms_category_id = $res['cms_category_id'];
			$cms_temp->description = $res['description'];
			$cms_temp->views_count = 0;
			$cms_temp->link = $res['link'];
			if ($res['is_active'])
				$cms_temp->is_active = 1;
			else
				$cms_temp->is_active = 0;
			$cms_temp->content = $res['content'];
			$cms_temp->created_at = date(DATE_ISO8601);
			$cms_temp->updated_at = date(DATE_ISO8601);
			
			try {
				$cms_temp->save();
				$this->create_success = true;
			} catch (sfException $e) {
				echo $e->getMessage();
			}
			
			if ($res["tags"] != "")
			{
				$res["tags"] = str_replace(";", ",", $res["tags"]);
				$tags_array = explode($res["tags"], ",");
				
				foreach ($tags_array as $tag) {
					$tag = trim($tag);
					if ($tag != "" && $tag != ",")
					{
						$cms_tag = new CmsTags();
						$cms_tag->cms_id = $cms_temp->cms_id;
						$cms_tag->tag = $tag;
						$cms_tag->save();
					}
				}
			}
			
			try {
				$files = $this->getRequest()->getFiles();
				$file = $files['cms']['picture'];
				if ($file['size'] > 0)
				{
					$tmpName = $file['tmp_name'];
					$fileName = $file['name'];
					$fileExt = trim(strrchr($fileName,'.'));
                    
                    $fileName =($cms_temp->cms_id).$fileExt;
					$img = new sfImage($tmpName, $file['type']);
					$img->saveAs(sfConfig::get('sf_upload_dir').'/cms/'.$fileName, $file['type']);
                    
                    foreach($cms_temp->dimensions as $dimensions) {
                        $fileName1 = $dimensions['name'].($cms_temp->cms_id).$fileExt;                    
					    $img = new sfImage($tmpName, $file['type']);
                        if($dimensions['width'] != 0 && $dimensions['height'] != 0 )
                            $img->thumbnail($dimensions['width'],$dimensions['height'],'center');
                        $img->setQuality(100);
					    $img->saveAs(sfConfig::get('sf_upload_dir').'/cms/'.$fileName1, $file['type']);                            
                    }
					$cms_temp->picture = $fileName;
					$cms_temp->save();
					$this->upload_success = true;
				}
			} catch (sfException $e) {
				echo $e->getMessage();
              }
			
		}
		
	} else {
		
		$q = Doctrine_Query::create()
			    ->from('VTNSCms c')
		        ->where('c.cms_category_id = ?', $request->getParameter('cms_category_id'));
		$this->cms_category_id = $request->getParameter('cms_category_id');	
		$res = $request->getParameter('cms');
		$q = $this->execFilers($q, $res);
		$this->sort_by = $request->getParameter('sort_by', 'created_at');
    	if ($request->getParameter('sort_order', 1))
        	$this->sort_order = 'desc';
    	else
        	$this->sort_order = 'asc';
			
    	if (Doctrine_Core::getTable('VTNSCms')->hasField($this->sort_by))
        	$q->orderBy('c.'.$this->sort_by.' '.strtoupper($this->sort_order));
			
		$this->pager = new sfDoctrinePager('VTNSCms', 10000);
		$this->pager->setQuery($q);
    	$this->pager->setPage($request->getParameter('page', 1));
    	$this->pager->init();
		if ($request->isXmlHttpRequest()) {
        	return 'Ajax';
    	} else return SfView::SUCCESS;
	}
  }


	private function execFilers($q, $res)
	{
		if ($res['name'] != '')
			$q->addWhere('c.name LIKE "%'.$res['name'].'%"');
		return $q;
	}
}
